{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "线性回归: \n",
      " 各特征系数 [-5.51707724e+16 -5.51707724e+16 -5.51707724e+16 -5.51707724e+16\n",
      "  1.22510449e+16  1.22510449e+16  1.22510449e+16  1.22510449e+16\n",
      "  1.22510449e+16  1.22510449e+16  1.22510449e+16  1.22510449e+16\n",
      "  1.22510449e+16  1.22510449e+16  1.22510449e+16  1.22510449e+16\n",
      " -3.76256303e+16 -3.76256303e+16 -3.76256303e+16  4.18436274e+16\n",
      "  6.66612515e+15  6.66612515e+15  6.66612515e+15  6.66612515e+15\n",
      "  6.66612515e+15  4.18436274e+16  2.27600000e+03  1.36000000e+03\n",
      " -1.53900000e+03 -1.40800000e+03  3.51775022e+16  3.51775022e+16\n",
      "  2.02275000e+03]   截距 3.870173042849784e+16 \n",
      "\n",
      "通过数学公式，RMSE测试集上评估结果：  731.6319471718338 \n",
      "\n",
      "Ridge回归: \n",
      " 各特征系数 [ -855.18275319    35.98613183   126.74742735   692.44919401\n",
      "  -430.32582693  -242.61807133   184.1700077    109.71713261\n",
      "   470.75992611   251.02712776  -297.22253412    12.30094502\n",
      "   639.08226209   204.49783472  -461.01416353  -440.37464012\n",
      "   786.16502273   261.83874652 -1048.00376925  -171.9287692\n",
      "  -175.27468607   -27.96696062    -9.06400966    40.963663\n",
      "    95.98116184   247.28960072  1783.01829331  1617.79222806\n",
      " -1263.82521125 -1275.09794785  -246.90056576   171.53973424\n",
      "  2042.27767234]   截距 2271.2906457779586 \n",
      "\n",
      "通过数学公式，RMSE测试集上评估结果：  722.7487041425906 \n",
      "\n",
      "Lasso回归: \n",
      " 各特征系数 [ -943.5284252     -0.             0.           598.75900967\n",
      "  -366.0962647   -196.35646716   168.88602627    27.34186716\n",
      "   356.09166241   102.76208641  -390.36987441   -55.38649183\n",
      "   600.56906272   178.4684262   -435.72460489  -396.50535139\n",
      "   492.99952441    -0.         -1303.42120332  -321.91223943\n",
      "  -162.10703512    -6.50978871     0.            38.98261446\n",
      "    93.21426504    88.1856628   2373.74304439  1316.30498538\n",
      " -1446.10535206 -1361.48805691  -406.19541449     2.54022421\n",
      "  2040.98871232]   截距 2817.7489053952613 \n",
      "\n",
      "通过数学公式，RMSE测试集上评估结果：  722.0564422919812 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "#对全体数据，随机选择其中80%做训练数据，剩下20%为测试数据，评价指标为RMSE。\n",
    "#用训练数据训练最小二乘线性回归模型、岭回归模型、Lasso模型，注意岭回归模型和Lasso模型的正则超参数调优。 \n",
    "#比较用上述三种模型得到的各特征的系数，以及各模型在测试集上的性能。\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "#读入数据\n",
    "FEtrain=pd.read_csv(\"C:/Users/123/Desktop/BikeSharing/FE_day.csv\")\n",
    "features=['season_1','season_2','season_3','season_4','mnth_1','mnth_2','mnth_3','mnth_4','mnth_5','mnth_6','mnth_7','mnth_8','mnth_9','mnth_10','mnth_11','mnth_12','weathersit_1','weathersit_2','weathersit_3','weekday_0','weekday_1','weekday_2','weekday_3','weekday_4','weekday_5','weekday_6','temp','atemp','hum','windspeed','holiday','workingday','yr']\n",
    "\n",
    "#对全体数据，随机选择其中80%做训练数据，剩下20%为测试数据\n",
    "#使用train_test_split对数据做8/2分\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test ,y_train, y_test= train_test_split(FEtrain[features], FEtrain['cnt'],test_size=0.2, random_state = 20, shuffle=True)\n",
    "\n",
    "#用训练数据训练最小二乘线性回归模型\n",
    "from sklearn import linear_model\n",
    "lr = linear_model.LinearRegression()\n",
    "lr.fit(X_train, y_train)  #训练数据集\n",
    "print(\"线性回归:\",\"\\n\",\"各特征系数\",lr.coef_,\" \",\"截距\",lr.intercept_,\"\\n\")\n",
    "y_test_pred=lr.predict(X_test)  #测试数据\n",
    "rmse_test=(np.sum((y_test_pred-y_test)**2)/len(y_test)) ** 0.5  #通过数学公式所做的RMSE评估\n",
    "print(\"通过数学公式，RMSE测试集上评估结果： \",rmse_test,'\\n')\n",
    "\n",
    "#用训练数据训练岭回归模型\n",
    "from sklearn import linear_model\n",
    "reg = linear_model.RidgeCV(alphas=[0.1, 1.0, 10.0], cv=5)\n",
    "reg.fit(X_train,y_train)       \n",
    "print(\"Ridge回归:\",\"\\n\",\"各特征系数\",reg.coef_,\" \",\"截距\",reg.intercept_,\"\\n\")\n",
    "y_test_pred2=reg.predict(X_test)  #测试数据\n",
    "rmse_test2=(np.sum((y_test_pred2-y_test)**2)/len(y_test)) ** 0.5  #通过数学公式所做的RMSE评估\n",
    "print(\"通过数学公式，RMSE测试集上评估结果： \",rmse_test2,'\\n')\n",
    "\n",
    "#用训练数据训练Lasso模型\n",
    "from sklearn.linear_model import LassoCV\n",
    "#from sklearn import metrics\n",
    "#from sklearn.model_selection import cross_validate\n",
    "reg2 = LassoCV(alphas=[0.1, 1.0, 10.0],cv=5, random_state=0)\n",
    "reg2.fit(X_train, y_train)\n",
    "print(\"Lasso回归:\",\"\\n\",\"各特征系数\",reg2.coef_,\" \",\"截距\",reg2.intercept_,\"\\n\")\n",
    "y_test_pred3=reg2.predict(X_test)  #测试数据\n",
    "rmse_test3=(np.sum((y_test_pred3-y_test)**2)/len(y_test)) ** 0.5  #通过数学公式所做的RMSE评估\n",
    "print(\"通过数学公式，RMSE测试集上评估结果： \",rmse_test3,'\\n')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
